Skip to main content

All Questions

-1votes
1answer
361views

In which cases and examples String in Java is not immutable? [duplicate]

If Strings are immutable in Java, why is the output of this code 2GB, instead of 1GB? class Laptop { String memory = "1GB"; } class Workshop { public static void main(String args[]) { ...
Nemus's user avatar
1vote
2answers
1kviews

What's the difference of an object being final and an object being immutable in java?

final String str = "do not change me"; str = "why not?"; //it will result in compile time error saying that final fields can not be re-assigned once created i.e. the reference cannot be changed ...
Rushi Shah's user avatar
86votes
11answers
34kviews

Why is String immutable in Java?

I couldn't understand the reason of it. I always use String class like other developers, but when I modify the value of it, new instance of String created. What might be the reason of immutability ...
yfklon's user avatar
  • 1,772
8votes
5answers
7kviews

A string is immutable, so why are they not all constants?

The string type is immutable. We can use the const keyword with strings in high level language like .NET. My understanding of 'const' means constant (it remains the same, we can't change the value). ...
Dave's user avatar
  • 672

close